package com.emath.objects;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.os.IBinder;
import android.widget.Toast;

import com.lnl.R;
import com.emath.shared.Globals;

public class BackgroundSoundService extends Service implements
		OnCompletionListener, OnPreparedListener, OnErrorListener,
		OnSeekCompleteListener, OnInfoListener, OnBufferingUpdateListener {
	private MediaPlayer mediaPlayer = new MediaPlayer();
	private String sntAudioLink;
	@Override
	public void onCreate() {
		mediaPlayer.setOnCompletionListener(this);
		mediaPlayer.setOnErrorListener(this);
		mediaPlayer.setOnPreparedListener(this);
		mediaPlayer.setOnBufferingUpdateListener(this);
		mediaPlayer.setOnSeekCompleteListener(this);
		mediaPlayer.setOnInfoListener(this);
		mediaPlayer.reset();

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// sntAudioLink = intent.getExtras().getString("sentAudioLink");
		// mediaPlayer.reset();

		if (!mediaPlayer.isPlaying()) {
			try {
				mediaPlayer = MediaPlayer.create(this, R.raw.synthrharbinger);
				mediaPlayer.setLooping(true);
				// mediaPlayer.setDataSource(sntAudioLink);
				mediaPlayer.start();
				Globals.isMusicPlaying = true;
				// mediaPlayer.prepareAsync();

			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			}
		}
		return START_STICKY;
	}

	public void onDestroy() {
		super.onDestroy();
		if (mediaPlayer != null) {
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.stop();
			}
			mediaPlayer.release();
		}
	}

	@Override
	public void onBufferingUpdate(MediaPlayer mp, int percent) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onInfo(MediaPlayer mp, int what, int extra) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onSeekComplete(MediaPlayer mp) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		switch (what) {
		case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
			Toast.makeText(this,
					"MEDIA ERROR NOT VALID FOR PROGRESSIVE PLAYBACK " + extra,
					Toast.LENGTH_LONG).show();
			break;
		case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
			Toast.makeText(this, "MEDIA ERROR SERVER DIED " + extra,
					Toast.LENGTH_LONG).show();
			break;
		case MediaPlayer.MEDIA_ERROR_UNKNOWN:
			Toast.makeText(this, "MEDIA ERROR UNKNOWN " + extra,
					Toast.LENGTH_LONG).show();
			break;
		}
		return false;
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		playMedia();

	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		stopMedia();
		stopSelf();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	public void playMedia() {
		if (!mediaPlayer.isPlaying()) {
			mediaPlayer.start();
			Globals.isMusicPlaying = true;
		}
	}

	public void stopMedia() {
		if (mediaPlayer.isPlaying()) {
			mediaPlayer.stop();
			Globals.isMusicPlaying = false;
		}
	}

}
